package com.financing.controller.app;
import java.util.Date;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;

import com.financing.model.Data;
import com.financing.model.app.AppPush;
import com.financing.service.app.AppPushService;
import com.financing.util.Json;
import com.financing.util.Str;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class AppPushController extends Controller {

	private static AppPushService appPush = new AppPushService();
	
	/**
	 * 消息推送
	 * @throws Exception
	 */
	@RequiresRoles(logical=Logical.OR,value={"admin","admin1"})  
	public void index() throws Exception {
		String title = getPara("title");
		String type = getPara("type","");
		String contents = getPara("contents");
		int isTimingPush = getParaToInt("isTimingPush",0);
		if(Str.nb(title) && Str.nb(type)) {
			if(isTimingPush==0) {
				AppPushService.send(title, contents, type);
			}
			AppPush push = new AppPush();
			push.setTitle(title);
			push.setContents(contents);
			push.setCreateTime(new Date());
			push.setIsTimingPush(isTimingPush);
			push.setPushTime(getParaToDate("pushTime",new Date()));
			push.save();
			renderJson(Json.ok("消息推送成功！"));
		}else {
			renderJson(Json.error("参数不可为空！"));
		}
	} 
	
	/**
	 * 交易记录列表
	 */
	public void list() { 
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Page<AppPush> p = appPush.list(page,size,Data.flat(getParaMap()));
		renderJson(Json.ok("数据获取成功！", Data.put("page", p)));
	}
}